<!--
*  2021-03-15
*  坯布入库列表
-->
<template>
  <div id="app" class="store-in">
    <!-- 头部导航 -->
    <van-nav-bar
      ref="header"
      left-arrow
      @click-left="onClickLeft"
      left-text="返回"
    >
      <template #title>
        <span @click="onClickPrev">前一天</span>
        <span @click="show = true" style="margin: 0 10px;">{{ dateTime }}</span>
        <span @click="onClickNext">后一天</span>
      </template>
      <template #right>
        <span @click="onClickRight"><van-icon name="plus" size="20" /></span>
        <!-- <span @click="onClickNext">后一天</span> -->
      </template>
    </van-nav-bar>
    <!-- 筛选 -->
    <van-search
      v-model="designNo"
      placeholder="请输入搜索关键词"
      @search="onClickSearch"
    />
    <!-- 总计 -->
    <van-cell
      class="outbound-list__total"
      v-show="tableData.length !== 0"
      :title="'总计匹数'"
      :value="totalNum"
    >
    </van-cell>
    <!-- 滚动 -->
    <better-scroll
      class="public-scroll"
      :data="tableData"
      :totalPage="totalPage"
      :pulldown="true"
      @pulldown="onGetYarnStoreOutList"
    >
      <van-cell-group v-show="tableData.length !== 0">
        <div v-for="(data, index) in tableData" :key="index">
          <div
            v-for="(d, key, i) in data"
            :key="i"
            class="detailed-operate__wrapper"
          >
            <van-cell v-if="i === 0" :title="'编号: ' + key">
              <template slot="label">
                <div>
                  <van-cell
                    v-for="(dd, dIndex) in d"
                    :key="dd.id"
                    :title="dIndex + 1 + '、长度: ' + dd.length"
                  >
                    <template slot="default">
                      <span> {{ "重量: " + dd.weight }} </span>
                    </template>
                  </van-cell>
                </div>
              </template>
              <template slot="default">
                <div>
                  <span>{{ "总匹数: " + d.length }}</span>
                </div>
              </template>
            </van-cell>
          </div>
        </div>
      </van-cell-group>
      <van-empty
        class="custom-image"
        image="https://img01.yzcdn.cn/vant/custom-empty-image.png"
        v-show="tableData.length === 0"
        description="暂无数据"
      />
    </better-scroll>

    <!-- 日期筛选 -->
    <van-calendar
      v-model="show"
      :min-date="minDate"
      :max-date="maxDate"
      :round="false"
      @confirm="onConfirm"
    />
  </div>
</template>

<script>
import { openWin, comeBack, getFloat } from "@/utils/index";
import { getClothStoreInList } from "@/api/cloth";
import dayjs from "dayjs";

export default {
  name: "greyClothStoreIn",
  data() {
    return {
      title: "",
      currentPage: 1,
      totalPage: 1,
      tableData: [],
      pageNum: 1,
      pageSize: 200,
      show: false,
      designNo: "",
      dateTime: dayjs().format("YYYY-MM-DD"),
      minDate: new Date(dayjs().subtract(1, "year")),
      maxDate: new Date(),
      beginAt: dayjs().format("YYYY-MM-DD 00:00:00"),
      endAt: dayjs().format("YYYY-MM-DD 24:00:00"),
      totalWeight: 0,
      totalNum: 0,
      totalSpindleNum: 0,
      totalLength: 0,
    };
  },
  watch: {
    // 监听事件筛选
    dateTime() {
      this.onGetYarnStoreOutList();
    },
  },
  mounted() {
    const that = this;
    if (process.env.NODE_ENV === "production") {
      this.$nextTick(() => {
        that.initFn(that);
      });
    } else {
      window.apiready = function () {
        that.initFn(that);
      };
    }
  },
  methods: {
    getFloat,

    //初始化
    initFn(that) {
      that.onGetYarnStoreOutList();
      // 监听页面消失的时候，关掉页面就可以了
      window.api.addEventListener({
        name: "viewdisappear",
      }, function () {
        window.api.closeWin();
      });
    },

    // 返回
    onClickLeft() {
      comeBack();
    },
    // 前一天
    onClickPrev() {
      this.dateTime = dayjs(this.dateTime)
        .subtract(1, "day")
        .format("YYYY-MM-DD");
    },
    // 后一天
    onClickNext() {
      if (this.dateTime === dayjs().format("YYYY-MM-DD")) {
        this.$toast.fail("明天还没到");
        return false;
      }
      this.dateTime = dayjs(this.dateTime).add(1, "day").format("YYYY-MM-DD");
    },
    // 时间筛选
    onConfirm(val) {
      this.dateTime = dayjs(val).format("YYYY-MM-DD");
      this.show = false;
    },
    // 扫码
    onClickRight() {
      openWin("in-details");
    },
    // 入库列表 // 下拉刷新
    onGetYarnStoreOutList() {
      this.tableData = [];
      this.totalNum = 0;
      this.totalWeight = 0;
      getClothStoreInList(
        {
          beginAt: dayjs(this.dateTime).format("YYYY-MM-DD 00:00:00"),
          endAt: dayjs(this.dateTime)
            .add(1, "day")
            .format("YYYY-MM-DD 00:00:00"),
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          designNo: this.designNo,
        },
        (res) => {
          this.totalNum = res.list.length;
          this.tableData = this.handlerDatas(res.list);
        }
      );
    },
    // 筛选
    onClickSearch() {
      this.pageNum = 1;
      this.onGetYarnStoreOutList();
    },
    // 数据处理
    handlerDatas(arr) {
      let tempArr = [];
      let afterData = [];
      for (let i = 0; i < arr.length; i++) {
        let item = arr[i];
        let key = item.designNo;
        if (tempArr.indexOf(key) === -1) {
          afterData.push({
            [item.designNo]: [item],
            totalLength: item.length,
            totalWeight: item.weight,
          });

          tempArr.push(key);
        } else {
          for (let j = 0; j < afterData.length; j++) {
            for (var designNo in afterData[j]) {
              if (designNo == arr[i].designNo) {
                afterData[j][designNo].push(arr[i]);
                let totalLength =
                  Number(afterData[j].totalLength) + Number(arr[i].length);
                let totalWeight =
                  Number(afterData[j].totalWeight) + Number(arr[i].weight);

                afterData[j].totalLength = !isNaN(parseFloat(totalLength))
                  ? totalLength.toFixed(2)
                  : totalLength;
                afterData[j].totalWeight = !isNaN(parseFloat(totalWeight))
                  ? totalWeight.toFixed(2)
                  : totalWeight;
                break;
              }
            }
          }
        }
      }
      return afterData; // 最终输出
    },
    // 计算总重量/总长度
    countTotal(arr, keyName) {
      let $total = 0;
      $total = arr.reduce(function (total, currentValue) {
        console.log(currentValue[keyName]);
        return currentValue[keyName]
          ? total + Number(currentValue[keyName])
          : Number(total);
      }, 0);
      return $total;
    },
  },
};
</script>

<style lang="scss">
.store-in {
  .van-search {
    position: relative;
    z-index: 999;
  }
  .van-nav-bar__title {
    display: flex;
    height: 100%;
    align-items: center;
    span {
      display: flex;
      height: 100%;
      align-items: center;
      justify-content: center;
      flex: 1;
      font-size: 14px;
      &:first-child {
        color: #1989fa;
      }
      &:last-child {
        color: #ee0a24;
      }
    }
  }
  .van-nav-bar__right {
    span {
      display: flex;
      height: 100%;
      align-items: center;
    }
  }
  .outbound-list__total {
    position: relative;
    z-index: 999;
    .van-cell__title {
      flex: 0 0 40%;
    }
    .van-cell__value {
      & > div {
        width: 100%;
        display: flex;
        justify-content: space-between;
      }
    }
    span {
      color: #1989fa;
      font-size: 16px;
    }
  }
  .public-scroll {
    height: calc(100vh - 174px);
    overflow: hidden;

    .van-cell {
      .van-cell__value {
        flex: 0 0 30%;
        div {
          display: flex;
          flex-wrap: wrap;
          flex-direction: column;
          span {
            display: flex;
            flex: 1;
            justify-content: center;
            &:last-child {
              justify-content: flex-end;
            }
          }
        }
      }
    }
  }
}
</style>

